""""
'''
从文件中读取数据
'''
"""
import os
import numpy as np
import math
import tensorflow as tf
from classification.config.Params import Params


params = Params()
train_image_list = []
train_label_list = []
test_image_list = []
test_label_list = []


# 返回的是一组图像的路径， labels是对应路径下的类型标签
def get_files(file_dir):
    for file in os.listdir(file_dir + "train"):
        train_image_list.append(file_dir + "train/" + file)
        train_label_list.append(int(file.split("_")[3].split(".")[0]))

    for file in os.listdir(file_dir + "test"):
        test_image_list.append(file_dir + "test/" + file)
        test_label_list.append(int(file.split("_")[3].split(".")[0]))
    # 利用shuffle打乱顺序
    train_temp = np.array([train_image_list, train_label_list])
    train_temp = train_temp.transpose()
    np.random.shuffle(train_temp)

    test_temp = np.array([test_image_list, test_label_list])
    test_temp = test_temp.transpose()

    # tra_images = list(train_temp[:, 0])
    # tra_labels = list(train_temp[:, 1])
    #
    # val_images = list(test_temp[:, 0])
    # val_labels = list(test_temp[:, 1])

    # return tra_images, tra_labels, val_images, val_labels
    return train_image_list, train_label_list, test_image_list, test_label_list


# 返回的是一个tensor， shape=（100， 64， 64， 3）
def get_batch(image, label, shape, batch_size, capacity):
    print(image)
    image = tf.cast(image, tf.string)
    label = tf.cast(label, tf.int32)

    input_queue = tf.train.slice_input_producer([image, label])

    label = input_queue[1]
    image_contents = tf.read_file(input_queue[0])

    image = tf.image.decode_jpeg(image_contents, channels=3)
    print(image)
    image = tf.image.resize_images(image, shape)
    image = tf.image.per_image_standardization(image)
    print(image)
    image_batch, label_batch = tf.train.batch([image, label], batch_size=batch_size, num_threads=32, capacity=capacity)
    label_batch = tf.reshape(label_batch, [batch_size])
    image_batch = tf.cast(image_batch, tf.float32)
    return image_batch, label_batch


if __name__ =="__main__":
    train, train_label, val, val_label = get_files(params.samples_path)
    # 训练数据及标签
    train_batch, train_label_batch = get_batch(train, train_label, params.shape, params.batch_size,
                                                         params.capacity)
    print(train_batch)
